<?php
// Below is a very simple PHP 5 script that hosts the RPX sign-in interface, and also acts as the token_url.
// If a token is present it processes the token using the backchannel auth_info API call.  Otherwise it simply
// renders the RPX interface using the embeded iframe.


/**
 * Resulting JSON for successfull login
 * {
 *	"profile":{
 *	    "verifiedEmail":"name@example.com",
 *	    "name":{
 *		"givenName":"Bob",
 *		"familyName":"Terwiliger",
 *		"formatted":"Bob Terwiliger"
 *	    },
 *	    "displayName":"display.name",
 *	    "preferredUsername":"prefered.name",
 *	    "providerName":"Google",
 *	    "identifier":"https:\/\/www.google.com\/accounts\/o8\/id?id=AItOawltWaahjIonILWj2ZHlmKUW8KpJAM8X6Q",
 *	    "email":"name@example.com"
 *	},
 *	"stat":"ok"
 *  }
 */
require './lib/core/Auth.php';
$auth = new Auth();
?>

<html>
    <head></head>
    <body>
	<h1>RPX PHP Example</h1>

	<?php
	if(isset($_POST['token'])) { // recieving a POST back from RDX
	    echo '<div style="background-color:#eee;border:1px solid #aaa;width:400px;padding:5px;">';
	    echo '<h3 style="color:green;margin:0;">Results</h3>';

	    $auth_info = $auth->login($_POST['token']);

	    // process the auth_info response
	    if ($auth_info) {

		$profile = $auth_info['profile'];
		echo "<pre>".print_r($auth_info['raw_json'])."</pre>";
		if (isset($profile['photo'])) {
		    echo '<img style="float:right;" src="' . $profile['photo'] . '"/><br/>';
		}

		if (isset($profile['displayName'])) {
		    echo '<b>Name:</b> ' . $profile['displayName'] . '<br/>';
		}

		if (isset($profile['email'])) {
		    echo '<b>Email:</b> ' . $profile['email'] . '<br/>';
		}

		// identifer is always present in the response
		echo '<b>Identifier: </b>' . $profile['identifier'] . '<br/>';

	    }
	    else {
		echo '<b>Error:</b> ' . $auth_info['err']['msg'];
	    }
	    
    ?>

	<div style="clear:both;"></div>
	<br/>
	<a href="<?php echo $auth->token_url();
	    ?>">Sign out</a>

    <?php
} 
	else { // initially, just display the login form
    ?>

	<h3>Sign in</h3>
	<!-- add the iframe based rpx interface -->
    <?php echo $auth->login_link();
    ?>


    <?php
}
?>
	<script src="https://rpxnow.com/openid/v2/widget"
	type="text/javascript"></script>
	<script type="text/javascript">
	    RPXNOW.flags = "";
	    RPXNOW.overlay = true;
	    RPXNOW.language_preference = 'en';
	</script>

    </body>
</html>